package biz;

import base.Result;
import comm.Constrant;
import dao.UserDao;
import org.apache.ibatis.session.SqlSession;
import pojo.User;
import util.SqlSessionFactoryUtil;

import java.util.List;

import static comm.Constrant.PAGE_SIZE;

public class UserService {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    UserDao dao = session.getMapper(UserDao.class);

    private static UserService service = new UserService();

    private UserService() {
    }

    public static UserService start() {
        return service;
    }

    /**
     * 管理员登录
     *
     * @param user
     * @return
     */
    public Result<User> adminSignIn(User user) {
        Result<User> result = new Result<>();
        User callback;
        try {
            callback = dao.signIn(user);
            if (callback == null) {
                result.setCode(Constrant.CODE_LOGIN_FAIL);
                result.setMessage(Constrant.errorMessage(Constrant.CODE_LOGIN_FAIL));
            } else {
                if (callback.getUserIdentity() == 1) {
                    result.setCode(Constrant.CODE_SUCCESS);
                    result.setData(callback);
                } else {
                    result.setCode(Constrant.CODE_ADMIN_ERROR);
                    result.setMessage(Constrant.errorMessage(Constrant.CODE_ADMIN_ERROR));
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(Constrant.CODE_LOGIN_FAIL);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 登录操作
     *
     * @param user
     * @return
     */
    public Result<User> signIn(User user) {
        Result<User> result = new Result<>();
        User callback;
        try {
            callback = dao.signIn(user);
            if (callback == null) {
                result.setCode(Constrant.CODE_LOGIN_FAIL);
                result.setMessage(Constrant.errorMessage(Constrant.CODE_LOGIN_FAIL));
            } else {
                result.setCode(Constrant.CODE_SUCCESS);
                result.setData(callback);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(Constrant.CODE_LOGIN_FAIL);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 注册操作
     *
     * @param user
     * @return
     */
    public Result<String> signUp(User user) {
        int callback;
        Result<String> result = new Result<>();
        try {
            if (getUserInfo(user).getCode() != Constrant.CODE_SUCCESS) {//先检查是否存在用户名
                callback = dao.signUp(user);
                System.out.println("-----------");
                session.commit();
                if (callback > 0) {
                    result.setCode(Constrant.CODE_SUCCESS);
                } else {
                    result.setCode(Constrant.CODE_REGISTER_FAIL);
                    result.setMessage(Constrant.errorMessage(Constrant.CODE_REGISTER_FAIL));
                }
            } else {
                result.setCode(Constrant.CODE_EXISTED_USER);
                result.setMessage(Constrant.errorMessage(Constrant.CODE_EXISTED_USER));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(Constrant.CODE_REGISTER_FAIL);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 获取用户信息
     *
     * @param user
     * @return
     */
    public Result<User> getUserInfo(User user) {
        Result<User> result = new Result<>();
        User callback;
        try {
            callback = dao.userInfo(user);
            if (callback == null) {
                result.setCode(Constrant.CODE_USER_INFO_FAIL);
                result.setMessage(Constrant.errorMessage(Constrant.CODE_USER_INFO_FAIL));
            } else {
                result.setCode(Constrant.CODE_SUCCESS);
                result.setData(callback);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(Constrant.CODE_USER_INFO_FAIL);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 获取用户列表
     */
    public Result<List<User>> getUserList(Object page) {
        Result<List<User>> result = new Result<>();
        try {
            List<User> data = dao.userList();
            if (data == null) {
                result.setCode(Constrant.CODE_USER_INFO_FAIL);
                result.setMessage(Constrant.errorMessage(Constrant.CODE_USER_INFO_FAIL));
            } else {
                List<User> users = null;
                if (page != null) {
                    int pageIndex;
                    if ((int) page == -1) {
                        pageIndex = data.size();
                    } else {
                        pageIndex = (int) page;
                    }
                    int start = (pageIndex - 1) * PAGE_SIZE;
                    int end = pageIndex * PAGE_SIZE;
                    if (data.size() < end) {
                        end = data.size();
                        start = end - PAGE_SIZE;
                    }
                    users = data.subList(start, end);
                }
                result.setCode(Constrant.CODE_SUCCESS);
                result.setData(users);
                if (data.size() % PAGE_SIZE > 0) {
                    result.setTotal((data.size() / PAGE_SIZE) + 1);
                } else {
                    result.setTotal(data.size() / PAGE_SIZE);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setCode(Constrant.CODE_USER_INFO_FAIL);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    public void closeSession() {
        session.close();
    }
}
